package com.zhuifeng.smart.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zhuifeng.smart.model.WebsType;
import com.zhuifeng.smart.services.WebsTypeServices;
import com.zhuifeng.smart.services.impl.WebsTypeServicesImpl;

/**
 * Servlet implementation class IndexAction
 */
@WebServlet("/index.do")
public class IndexAction extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public IndexAction() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String indexType = request.getParameter("indexType");
		if ("home".equals(indexType)) {
			request.getRequestDispatcher("WEB-INF/pages/home.jsp").forward(
					request, response);
			return;
		} else if ("randImage".equals(indexType)) {
			request.getRequestDispatcher("WEB-INF/pages/image.jsp").forward(
					request, response);
			return;
		} else if ("logout".equals(indexType)) {
			HttpSession session = request.getSession(true);
			session.removeAttribute("userInfo");
		}
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=UTF-8");

		WebsTypeServices websTypeServices = new WebsTypeServicesImpl();
		List<WebsType> wList = websTypeServices.getWebsTypeList();
		request.setAttribute("wList", wList);

		request.getRequestDispatcher("WEB-INF/index.jsp").forward(request,
				response);
	}

}
